package com.vecspace.dayu.code.ui.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.vecspace.data.model.RowMapper;
import com.vecspace.database.dao.BaseDao;
import com.vecspace.dayu.code.ui.pojo.FormField;

/** DAYU-表单列,操作数据库的dao类
 * @author: wanghua
 */
@Repository("dayu_formFieldDao")
public class FormFieldDao extends BaseDao {
	/** 根据ID查询 DAYU-表单列
	 * @param id   主键值
	 * @return FormField
	 * @throws Exception
	 * @author wanghua
	 */
	public FormField getFormField(Long id) throws Exception {
		FormField formField = null;
		if (id != null && id != 0) {

			formField = this.queryObject(FormField.class, id);
			this.evict(formField);
		}
		return formField;
	}
	
	/**获取表单下的字段
	 * 
	 * @param formId
	 * @return
	 * @throws Exception 
	 * @author wanghua
	 */
	public List<FormField> getFormFields(Long formId) throws Exception {
		String sql = "SELECT t1.ID,t1.FORM_ID,t1.COLUMN_ID,t2.COLUMN_NAME,t1.INPUT_TIP,t1.COLUMN_LABEL,t1.COLUMN_GROUP,t1.NEW_ROW,t1.COL_SPAN,t1.ROW_SPAN,t1.FORM_ELEMENT_TYPE,t1.REQUIRED,t1.VALIDATION_RULES,\r\n" + 
				"t1.ORDNUM,t1.CREATE_TIME,t1.CREATE_USER FROM DAYU_CODE_ui_form_field t1 LEFT JOIN DAYU_CODE_data_column t2 ON t1.COLUMN_ID=t2.ID WHERE t1.FORM_ID='" + formId + "' ORDER BY t1.ORDNUM";
		List<Map<String, Object>> rows = queryDataRowMapList(sql);
		List<FormField> formFields = new ArrayList<>();
		for(Map<String, Object> row : rows){
			RowMapper rowmap = new RowMapper(row);
			FormField formField = new FormField();
			formField.setId(rowmap.getLong("ID"));
			formField.setFormId(rowmap.getLong("FORM_ID"));
			//formField.setColumnName(rowmap.getString("COLUMN_NAME"));
			formField.setColumnId(rowmap.getLong("COLUMN_ID"));
			formField.setInputTip(rowmap.getString("INPUT_TIP"));
			formField.setColumnLabel(rowmap.getString("COLUMN_LABEL"));
			formField.setColumnGroup(rowmap.getString("COLUMN_GROUP"));
			formField.setNewRow(rowmap.getBoolean("NEW_ROW"));
			formField.setColSpan(rowmap.getInteger("COL_SPAN"));
			formField.setRowSpan(rowmap.getInteger("ROW_SPAN"));
			formField.setFormElementType(rowmap.getString("FORM_ELEMENT_TYPE"));
			formField.setRequired(rowmap.getBoolean("REQUIRED"));
			formField.setValidationRules(rowmap.getString("VALIDATION_RULES"));
			formField.setOrdnum(rowmap.getInteger("ORDNUM"));
			formField.setCreateTime(rowmap.getDate("CREATE_TIME"));
			formField.setCreateUser(rowmap.getString("CREATE_USER"));
			formFields.add(formField);
		}
		return formFields;
	}

	/** 插入或修改 DAYU-表单列 
	 * @param item	要保存的对象
	 * @return void
	 * @throws Exception
	 * @author wanghua
	 */
	public void saveFormField(FormField item) throws Exception {
		this.saveOrUpdateObject(item);
		this.evict(item);
	}
	
	/**更新单个字段的值
	 * @param columnName
	 * @param id
	 * @param value
	 * @return 返回更新的记录数
	 * @throws Exception 
	 * @author wanghua
	 */
	public int updateValue(Long id, String columnName, Object value) throws Exception {
		String tableName = "DAYU_CODE_UI_FORM_FIELD";
		String idColumn = "ID";
		return super.updateValue(tableName, idColumn, columnName, id, value);
	}

	/** 根据ID删除 DAYU-表单列 
	 * @param id	主键值
	 * @return void
	 * @throws Exception
	 * @author wanghua
	 */
	public void deleteFormField(Long id) throws Exception {
		if (id != null && id != 0) {
			this.deleteObject(FormField.class, id);
		}
	}
}
